<template>
  <div class="dashboard">
    <!-- 两个选择下拉框 -->
    <el-row type="flex" justify="end">
      <el-select
        v-model="currentYear"
        size="small"
        placeholder="请选择"
        style="width: 120px"
        @change="dateChange"
      >
        <el-option v-for="item in yearList" :key="item" :value="item">{{
          item
        }}</el-option>
      </el-select>

      <el-select
        v-model="currentMonth"
        size="small"
        placeholder="请选择"
        style="width: 120px; margin-left: 10px"
        @change="dateChange"
      >
        <el-option v-for="item in 12" :key="item" :value="item">
          {{ item }}
        </el-option>
      </el-select>
    </el-row>
    <el-calendar v-model="currentDate">
      <template v-slot:dateCell="{ date, data }" class="content">
        <div class="date-content">
          <span class="text"> {{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>
<script>
export default {
  name: 'WorkCalendar',
  components: {},
  filters: {
    getDay(value) {
      const day = value.split('-')[2]
      return day.startsWith('0') ? day.substr(1) : day
    }
  },
  props: {
    //  接受开始日期
    startDate: {
      type: Date,
      //   默认值需要一个函数
      default: () => new Date()
    }
  },
  data() {
    //  获取到当前年份
    const currentYear = this.startDate.getFullYear()
    const currentMonth = this.startDate.getMonth() + 1
    const yearList = []
    //   利用 es5 来获取到 当前 年份前五年 与后五年的数据
    // 方式1-用es5
    // for (var i = -5; i <= 5; i++) {
    //   yearList.push(currentYear + i)
    // }
    // for (var i = 0; i <= 10; i++) {
    //   yearList.push(currentYear + i - 5)
    // }
    // 方式2-用es6
    // Array.from(new Array(11)).forEach((t, i) => {
    //   yearList.push(currentYear + i - 5)
    // })
    Array.from(new Array(11), (t, i) => yearList.push(currentYear + i - 5))
    return {
      // 我们只显示当前年份中的 前5年与后五年的 时间日期
      yearList,
      currentYear, // 当前年份
      currentMonth, // 当前月份
      currentDate: this.startDate
    }
  },
  computed: {},
  watch: {},
  created() {
    // 计算 当年当月的第一个周一 再加上 四周之后的一个月月份
    this.dateChange()
  },
  methods: {
    // 是否是休息日
    isWeek(value) {
      return value.getDay() === 6 || value.getDay() === 0
    },
    dateChange(value) {
      //   事件触发时 改变 日历绑定的日期
      console.log(value)
      this.currentDate = new Date(this.currentYear, this.currentMonth - 1)
    }
  }
}
</script>

<style  scoped>
/deep/ .el-calendar-day {
  height: auto;
}
/deep/ .el-calendar-table__row td,
/deep/ .el-calendar-table tr td:first-child,
/deep/ .el-calendar-table__row td.prev {
  border: none;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
/deep/ .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
/deep/ .el-calendar__header {
  display: none;
}
</style>

